﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PMSGSC.Common;
using DevExpress.XtraEditors;
using PMSGSC.DAL;
using PMSGSC.BLL;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Controls;
using System.Globalization;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data.Linq;
using DevExpress.Utils.OAuth.Provider;
using DevExpress.XtraRichEdit.Model;
using System.Diagnostics;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Localization;
using DevExpress.XtraTab;
using System.Threading;
namespace PMSGSC.App
{
    public partial class frmFilterComponent : DevExpress.XtraEditors.XtraForm
    {
        public frmFilterComponent(frmMain main)
        {
            InitializeComponent();
            objMain = main;
            ucLoadEquipment1.Focus();
        }
        bool isStop = true;
        private frmMain objMain;
        List<Equipment> componentList = new List<Equipment>();
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (isStop)
            {
                frmProcessBar pb = new frmProcessBar();
                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(() =>
                {
                    LoadSkinToForm();
                    pb.ShowDialog();
                }
                ));
                thread.Start();

                isStop = false;
                if (ucLoadEquipment1.txtName.Text != "" && ucLoadEquipment1.txtName.Text[0] == '$' && 
                    ucLoadEquipment1.txtName.Text[ucLoadEquipment1.txtName.Text.Length - 1] == '$')
                {
                    Search("");
                    string[] Name = ucLoadEquipment1.txtName.Text.Split('$');
                    GetResultSearchCompByName(Name[1]);
                }
                else
                {
                    Search(ucLoadEquipment1.txtName.Text);
                }

                if (componentList.Count > 0)
                {
                    frmComponent component = new frmComponent(componentList, objMain);
                    component.MdiParent = objMain;
                    component.Show();

                    if (pb.InvokeRequired)
                    {
                        pb.BeginInvoke(new Action(() => pb.Close()));
                        isStop = true;
                    }
                    this.Close();
                }
                else
                {
                    if (pb.InvokeRequired)
                    {
                        pb.BeginInvoke(new Action(() => pb.Close()));
                        isStop = true;
                    }
                    XtraMessageBox.Show("Not result in display", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        private static void OnlyInputNumberic(KeyPressEventArgs e)
        {
            const char Delete = (char)8;
            e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete;
        }

        private static void LoadSkinToForm()
        {
            DevExpress.Skins.SkinManager.EnableFormSkins();
            DevExpress.UserSkins.BonusSkins.Register();
            DevExpress.UserSkins.OfficeSkins.Register();
            DevExpress.LookAndFeel.DefaultLookAndFeel obj = new DevExpress.LookAndFeel.DefaultLookAndFeel();
            try
            {
                LookAndFeelSettings.Load("LookAndFeelSettings.dat");
            }
            catch
            {
                obj.LookAndFeel.SetSkinStyle("Blue");
            }
        }

        private void Search(string name)
        {
            Equipment equip = new Equipment();
            if (ucLoadEquipment1.lbid.Text == "")
            {
                equip.CodeEquipment = "";
                equip.NameEquipment = name;
            }
            else
            {
                equip.CodeEquipment = ucLoadEquipment1.txtCode.Text;
                equip.NameEquipment = name;
            }

            if (ucLoadMaker1.lb_id.Text == "")
                equip.Maker_ID = 0;
            else
                equip.Maker_ID = int.Parse(ucLoadMaker1.lb_id.Text);

            if (ucLoadVendor1.lbid.Text == "")
                equip.Vendor_ID = 0;
            else
                equip.Vendor_ID = int.Parse(ucLoadVendor1.lbid.Text);

            equip.Serial = txtSerial.Text;

            componentList = Equipment_BLL.SearchEquipment(equip, frmMain.ship.Ship_ID);
        }

        private void GetResultSearchCompByName(string name)
        {
            for (int i = 0; i < componentList.Count; i++ )
            {
                if (componentList[i].NameEquipment != null && !componentList[i].NameEquipment.ToLower().Contains(name.ToLower()))
                {
                    componentList.RemoveAt(i);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (isStop)
            {
                this.Close();
            }
        }

        private void txtSerial_KeyPress(object sender, KeyPressEventArgs e)
        {
            OnlyInputNumberic(e);
        }
    }
}